// pages/complain/index.js
import {
  platform
} from '../../api/apiData'
const upLoadUrl = getApp().globalData.upLoadUrl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    district: '',
    show: false,
    columns: [],
    contact_name: '',
    contact_phone: '',
    content: '',
    images: '',
    fileList: [],
    fileList2: [],
    fileListX: [],
    fileList2X: [],
    contact_nameX: '',
    contact_phoneX: '',
    contentX: '',
    district_id: '',
    address: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //   district.all({}, res => {
    //     this.setData({
    //       columns: res.data
    //     })
    //   })
  },
  // 数据绑定
  vmodel(e) {
    this.setData({
      [e.currentTarget.dataset.value]: e.detail.value
      /**
       * e.currentTarget.dataset.value 拿到我要改变的数据 input 
       * e.detail.value 拿到 我改变以后的值
       */
    })
  },
  // 上传图片
  afterRead(event) {
    let that = this
    const {
      file
    } = event.detail;
    console.log(file)
    file.forEach(item => {
      wx.uploadFile({
        url: upLoadUrl, // 仅为示例，非真实的接口地址
        filePath: item.url,
        name: 'file',
        formData: {
          user: 'test'
        },
        success(res) {
          console.log(res)
          const {
            fileList = [],
              fileList2 = []
          } = that.data;
          fileList.push({
            url: JSON.parse(res.data).data.url
          });
          fileList2.push(JSON.parse(res.data).data.path)
          console.log(fileList2)
          that.setData({
            fileList,
            fileList2,
          });
        },
      });
    })
  },
  deleteImg(event) {
    let index = event.detail.index
    let arr = this.data.fileList
    let arr2 = this.data.fileList2
    arr.splice(index, 1)
    arr2.splice(index, 1)
    this.setData({
      fileList: arr,
      fileList2: arr2
    })
  },
  afterReadX(event) {
    let that = this
    const {
      file
    } = event.detail;
    console.log(file)
    file.forEach(item => {
      wx.uploadFile({
        url: upLoadUrl, // 仅为示例，非真实的接口地址
        filePath: item.url,
        name: 'file',
        formData: {
          user: 'test'
        },
        success(res) {
          console.log(res)
          const {
            fileListX = [],
              fileList2X = []
          } = that.data;
          fileListX.push({
            url: JSON.parse(res.data).data.url
          });
          fileList2X.push(JSON.parse(res.data).data.path)
          console.log(fileList2X)
          that.setData({
            fileListX,
            fileList2X,
          });
        },
      });
    })
  },
  deleteImgX(event) {
    let index = event.detail.index
    let arr = this.data.fileListX
    let arr2 = this.data.fileList2X
    arr.splice(index, 1)
    arr2.splice(index, 1)
    this.setData({
      fileListX: arr,
      fileList2X: arr2
    })
  },
  toShow() {
    this.setData({
      show: true
    })
  },
  onConfirm(e) {
    let value = e.detail.value
    this.setData({
      district_id: value.id,
      district: value.name,
      show: false
    })
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  // 提交建议
  toSuggest() {
    let obj = {
      content: this.data.content,
      images: this.data.fileList2.toString(),
    }
    if (obj.content == '') {
      wx.showToast({
        title: '请输入建议内容',
        icon: 'error'
      })
      return false
    }
    wx.showModal({
      title: '提示',
      content: '确定要提交建议吗？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      confirmText: "是", //默认是“确定”
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框

        } else {
          //点击确定
          platform.feedback.submit(obj, res => {
            if (res.code == 1) {
              wx.showToast({
                title: res.msg,
              })
              setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/my/feedList',
                })
              }, 1000)
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
          })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  // 提交投诉
  toComplain() {
    let obj = {
      contact_name: this.data.contact_nameX,
      contact_phone: this.data.contact_phoneX,
      content: this.data.contentX,
      images: this.data.fileList2X.toString(),
      address: this.data.address,
      district_id: this.data.district_id
    }
    if (obj.contact_name == '') {
      wx.showToast({
        title: '请输入联系人',
        icon: 'error'
      })
      return false
    }
    if (obj.contact_phone == '') {
      wx.showToast({
        title: '请输入联系人电话',
        icon: 'error'
      })
      return false
    }
    if (obj.content == '') {
      wx.showToast({
        title: '请输入投诉事由',
        icon: 'error'
      })
      return false
    }
    wx.showModal({
      title: '提示',
      content: '确定要提交投诉吗？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      confirmText: "是", //默认是“确定”
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框

        } else {
          //点击确定
          complaint.submit(obj, res => {
            if (res.code == 1) {
              wx.showToast({
                title: res.msg,
              })
              setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/mySuggest/index?type=2',
                })
              }, 1000)
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
          })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})